package com.xiashitech.agent.dns;

import com.google.common.collect.Lists;
import io.grpc.Attributes;
import io.grpc.EquivalentAddressGroup;
import io.grpc.NameResolver;

import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class GrpcNameResolver extends NameResolver {
    private final String path;

    private final int port;
    private Listener2 listener;
    public GrpcNameResolver(String path, int port) {
        this.path = path;
        this.port = port;
    }
    @Override
    public String getServiceAuthority() {
        return this.path;
    }

    @Override
    public void shutdown() {

    }

    @Override
    public void start(Listener2 listener) {
        this.listener = listener;
        this.resolve();
    }
    private void resolve() {
        List<SocketAddress> socketAddressList = getAddressList(path, port).stream()
                .map(this::toSocketAddress)
                .collect(Collectors.toList());

        List<EquivalentAddressGroup> equivalentAddressGroups = Lists.newArrayList();
        for (SocketAddress socketAddress : socketAddressList) {
            EquivalentAddressGroup equivalentAddressGroup = new EquivalentAddressGroup(socketAddress);
            equivalentAddressGroups.add(equivalentAddressGroup);
        }
        ResolutionResult resolutionResult = ResolutionResult.newBuilder()
                .setAddresses(equivalentAddressGroups)
                .setAttributes(Attributes.EMPTY)
                .build();

        this.listener.onResult(resolutionResult);

    }
    private SocketAddress toSocketAddress(InetSocketAddress address) {
        return new InetSocketAddress(address.getHostName(), address.getPort());
    }
    private List<InetSocketAddress> getAddressList(String path, int port) {
        System.out.println("getAddressList called, path: " + path + ", " + port);
        List<InetSocketAddress> addresses = new ArrayList<>();
        InetAddress[] inetAddresses = CustomDns.getInstance().getDnsRealIps(path);
        for(InetAddress address: inetAddresses) {
            System.out.println("getAddressList InetAddress: " + address.getHostName() + ", " + address.getHostAddress() + "; " + address );
            // InetSocketAddress inetSocketAddress = new InetSocketAddress(address.getHostName(), port);
            // InetSocketAddress inetSocketAddress = new InetSocketAddress(address, port);
            InetSocketAddress inetSocketAddress = new InetSocketAddress(address.getHostAddress(), port);
            System.out.println("getAddressList inetSocketAddress: " + inetSocketAddress.getHostName() + ", " + inetSocketAddress.getAddress().getHostAddress() + ", " + inetSocketAddress.getPort() + "; " + inetSocketAddress);
            addresses.add(inetSocketAddress);
        }
        // InetAddress address = CustomDns.getInstance().getOneAddress(path);
        // InetSocketAddress inetSocketAddress = new InetSocketAddress(address.getHostAddress(), port);
        // addresses.add(inetSocketAddress);
        return addresses;
        // InetSocketAddress inetSocketAddress = new InetSocketAddress("localhost", 10084);
        // InetSocketAddress inetSocketAddress2 = new InetSocketAddress("localhost", 10085);
        // InetSocketAddress inetSocketAddress3 = new InetSocketAddress("localhost", 10086);
        // return Arrays.asList(inetSocketAddress, inetSocketAddress2,inetSocketAddress3);
    }
}
